package cn.common.mybatis;

public class MysqlTranslator extends DefaultTranslator{

	@Override
	public String sysdate() 
	{
		return "now()";
	}

	@Override
	public String nextvalSql(String seqName) 
	{
		return "SELECT _nextval('"+seqName+"')";
	}

	@Override
	public String nextval(String seqName) 
	{
		return "_nextval('"+seqName+"')";
	}

	@Override
	public String concat(String... str) 
	{
		StringBuilder sb=new StringBuilder();
		for(String s:str)
			sb.append(",").append(s);
		return "concat("+sb.substring(1)+")";
	}

	@Override
	public String nextvalN(String seqName, String n) 
	{
		return "call _nextvalN('"+seqName+"',"+n+")";
	}

	@Override
	public String limit(String count, boolean hasCriteria) 
	{
		return "limit "+count;
	}

	@Override
	public String strToDate(String dateStr, String format) 
	{
		if(format==null)
			return "";
			
		if("yyyy-MM-dd HH:mm:ss".equals(format))
			return "DATE_FORMAT("+dateStr+",'%Y-%m-%d  %H:%i:%s')";
		else if("yyyy-MM-dd".equals(format))
			return "DATE_FORMAT("+dateStr+",'%Y-%m-%d')";
		return "DATE_FORMAT("+dateStr+",'"+format+"')";
	}

	@Override
	public String keywordEscape(String keyword) 
	{
		return "`"+keyword+"`";
	}
}
